<template>
    <transition name="slider">
        <div class="down">
            <m-header :canreturn=true :type='5' @server='selectItem'></m-header>
            <div class="warpper" ref="downwrapper">
                <div class="down-ing" v-show="item === 0">
                    <div class="item" v-for="(task,index) in comtasks" :key="index">
                        <img v-lazy="task.icon" alt="" class="">
                        <div class="precent">
                            <div class="box" :style="{width:task.precent+'%'}"></div>
                            <p>
                                10M/S
                                <span>{{ task.precent}}%</span>
                            </p>
                        </div>
                        <div class="ctrl">
                            <div v-if="task.rate === 100" class="anzhuang" @click="install(task)">安装</div>
                            <div v-else>
                                <span class="stop" v-show="task.isstart" @click="stop(index)"></span>
                                <span class="start" v-show="!task.isstart" @click="start(index)"></span>
                            </div>
                        </div>
                    </div>
                    <button @click="addTask"> 添加任务</button>
                    <button @click="clear"> 清除任务</button>
                    <button @click="enumerate">枚举任务</button>
                </div>
                <div class="install" v-show="item === 1">
                    <div class="item" v-for="(game,index) in downcomplete" :key="index" @click="openGame(game)">
                        <img v-lazy="game.icon" alt="">
                        <!-- <p v-show="game.isinstall">打开</p>
                        <p v-show="!game.isinstall">安装</p> -->
                    </div>
                </div>
            </div>
        </div>
    </transition>
</template>

<script type="text/ecmascript-6">
import MHeader from '@/components/m-header/m-header';
import Scroll from '@/base/scroll/scroll';
import Loading from '@/base/loading/loading';
import { mapGetters, mapMutations } from 'vuex';
import { getLocalStorage } from 'common/js/storage';

export default {
    data() {
        return {
            item: 0,
            precent: 0,
            downGameTasks: [],
            downcomplete: [],
            gamelist: [],
        };
    },
    created() {
        this.enumerate();
    },
    mounted() {
        this.$refs.downwrapper.style.top = (44 + parseInt(this.statusbar)) + 'px';
    },
    methods: {
        selectItem(val) {
            this.item = val;
        },
        start(index) {
            this.gamelist[index].resume();
            this.$set(this.gamelist[index], 'isstart', true);
        },
        stop(index) {
            this.gamelist[index].pause();
            this.$set(this.gamelist[index], 'isstart', false);
        },
        openGame(game) {
            console.log('打开应用');
            console.log(plus.os.name);
            console.log(game.isinstall);
            if (game.isinstall) {
                if (plus.os.name === 'Android') {
                    console.log(game.pk_n);
                    plus.runtime.launchApplication({
                        pname: game.pk_n,
                    }, (e) => {
                        console.log('打开失败');
                        console.log(e);
                    });
                }
            } else {
                console.log(game.filename);
                this.install(game);
            }
        },
        install(game) {
            plus.runtime.install(game.filename, {}, () => {
                console.log('安装成功');
                this.$set(game, 'isinstall', true);
            }, (error) => {
                console.log('安装失败');
                this.$set(game, 'isinstall', false);
                console.log(error);
            });
        },
        enumerate() {
            let _this = this;
            console.log('进入');
            // 获取本地头像数据
            let avatar = getLocalStorage('icon');
            //  解析头像对象
            let avatarObject = avatar !== '' ? JSON.parse(avatar) : '';
            plus.downloader.enumerate((tasks) => {
                console.log('tasks--nullargs');
                console.log(tasks);
                let done = this.done + 1;
                _this.gamelist = tasks;
                for (let i = 0; i < _this.gamelist.length; i++) {
                    let gameName = _this.gamelist[i].filename;
                    _this.$set(_this.gamelist[i], 'icon', avatarObject[gameName].icon ? avatarObject[gameName].icon : '');
                    if (_this.gamelist[i].state === 3) {
                        _this.gamelist[i].pause();
                        _this.gamelist[i].addEventListener('statechanged', (task) => {
                            switch (task.state) {
                                case 3:
                                    // _this.$set(_this.gamelist[i], 'rate', Math.ceil(task.downloadedSize / task.totalSize * 100));
                                    break;
                                case 4:
                                    console.log('下载完成');
                                    this.setDone(done);
                                    console.log('#######');
                                    break;
                            }
                        });
                        _this.gamelist[i].resume();
                        _this.$set(_this.gamelist[i], 'isstart', true);
                    }
                    if (_this.gamelist[i].state === 5) {
                        // _this.$set(_this.gamelist[i], 'rate', Math.ceil(task.downloadedSize / task.totalSize * 100));
                        _this.$set(_this.gamelist[i], 'isstart', false);
                    }
                }
            });
        },
        addTask() {
            console.log('添加');
            let _this = this;
            let downTask = plus.downloader.createDownload(
                'http://download.yxorange.com/dzg/888890.apk',
                { method: 'GET' },
                //  下载完成执行的回调函数
                (d, status) => {
                    console.log(d);
                    console.log(status);
                    _this.install(d.filename);
                },
            );
            console.log(downTask);
            downTask.start();
        },
        clear() {
            plus.downloader.clear(1);
            plus.downloader.clear(2);
            plus.downloader.clear(3);
            plus.downloader.clear(4);
            plus.downloader.clear(5);
            plus.downloader.clear();
        },
        ...mapMutations([
            'setDone',
        ]),
    },
    computed: {
        comtasks() {
            for (let i = 0; i < this.gamelist.length; i++) {
                this.$set(this.gamelist[i], 'precent', Math.ceil(this.gamelist[i].downloadedSize / this.gamelist[i].totalSize * 100) > 100 ? 100 : Math.ceil(this.gamelist[i].downloadedSize / this.gamelist[i].totalSize * 100));
            }
            return this.gamelist;
        },
        ...mapGetters(['downgames', 'statusbar', 'done']),
    },
    components: {
        MHeader,
        Scroll,
        Loading,
    },
};
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
@import '~common/stylus/variable';
@import '~common/stylus/mixin';

slider();

.down {
    position: fixed;
    width: 100%;
    top: 0;
    bottom: 0;
    background: #ffffff;
    z-index: 20;

    .warpper {
        position: fixed;
        width: 100%;
        top: 44px;
        bottom: 0;
        background: #ffffff;
        z-index: 20;

        .down-ing {
            width: 100%;

            .item {
                display: flex;
                align-items: center;
                padding: 15px;
                border-bottom: 1px solid #eeeeee;
                position: relative;

                .precent {
                    height: 15px;
                    width: 65%;
                    box-sizing: border-box;
                    border: 1px solid #dea303;
                    border-radius: 15px;
                    margin-left: 15px;
                    position: relative;

                    .box {
                        width: 0;
                        height: 100%;
                        border-radius: 15px;
                        background: linear-gradient(#fbe098, #f8b500, #fad56e);
                    }

                    p {
                        position: absolute;
                        top: -14px;
                        right: 0;
                        font-size: 12px;
                        color: #999999;

                        span {
                            color: #222222;
                        }
                    }
                }

                .ctrl {
                    position: absolute;
                    right: 15px;
                    height: 50px;
                    top: 15px;
                    width: 10%;
                    display: flex;
                    justify-content: center;
                    align-items: center;

                    .anzhuang {
                        width: 36px;
                        height: 18px;
                        font-size: 12px;
                        background: #ffc400;
                        color: #ffffff;
                        text-align: center;
                        line-height: 18px;
                        border-radius: 9px;
                    }

                    span {
                        display: block;
                        height: 20px;
                        width: 20px;
                        background-size: 100% 100%;

                        &.stop {
                            bg-image('stop');
                        }

                        &.start {
                            bg-image('startdown');
                        }
                    }
                }

                img {
                    display: block;
                    width: 50px;
                    height: 50px;
                    border-radius: 5px;
                }
            }
        }

        .install {
            padding: 0 15px;
            display: flex;
            flex-wrap: wrap;

            .item {
                width: 32%;
                background: red;
                margin-right: 2%;
                padding: 15px;
                padding-bottom: 41px;
                margin-top: 15px;
                box-sizing: border-box;
                background: #eeeeee;
                border: 1px solid #cccccc;
                border-bottom: none;
                border-radius: 5px;
                text-align: center;
                position: relative;

                img {
                    width: 100%;
                    display: block;
                    border-radius: 10px;
                }

                &:nth-child(3n+0) {
                    margin-right: 0;
                }

                p {
                    position: absolute;
                    bottom: 0;
                    left: 0;
                    width: 100%;
                    height: 26px;
                    line-height: 26px;
                    text-align: center;
                    background: #ffc400;
                    color: #ffffff;
                }
            }
        }
    }
}
</style>
